package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.home.dto.ReportDrawingAuditHistoryDTO;
import com.eastfair.home.dto.ReportDrawingCategoryAuditHistoryDTO;
import com.eastfair.home.entity.ReportDrawingAudit;
import com.eastfair.venueservice.consts.CommonConstants;
import com.eastfair.venueservice.dao.ReportVenueDrawingCategoryAuditHistoryMapper;
import com.eastfair.venueservice.dto.ReportVenueDrawingAuditHistoryDTO;
import com.eastfair.venueservice.dto.ReportVenueDrawingCategoryAuditHistoryDTO;
import com.eastfair.venueservice.entity.ReportVenueDrawingAudit;
import com.eastfair.venueservice.entity.ReportVenueDrawingCategoryAudit;
import com.eastfair.venueservice.entity.ReportVenueDrawingCategoryAuditHistory;
import com.eastfair.venueservice.service.ReportVenueDrawingAuditHistoryService;
import com.eastfair.venueservice.service.ReportVenueDrawingCategoryAuditHistoryService;
import com.eastfair.venueservice.vo.ReportVenueDrawingCategoryAuditHistoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 一键报馆图纸分类审核记录历史
 * </p>
 *
 * @author dq
 * @date 2022-09-16
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportVenueDrawingCategoryAuditHistoryServiceImpl extends SuperServiceImpl<ReportVenueDrawingCategoryAuditHistoryMapper, ReportVenueDrawingCategoryAuditHistory> implements ReportVenueDrawingCategoryAuditHistoryService {

    @Resource
    private ReportVenueDrawingAuditHistoryService reportVenueDrawingAuditHistoryService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportVenueDrawingCategoryAuditHistory> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportVenueDrawingCategoryAuditHistory model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public Page<ReportVenueDrawingCategoryAuditHistoryVO> listHistoryVOs(PageParams<ReportVenueDrawingCategoryAuditHistoryDTO> pageParams) {
        Page<ReportVenueDrawingCategoryAuditHistory> page = listHistories(pageParams);
        Page<ReportVenueDrawingCategoryAuditHistoryVO> voPage = new Page<>();
        BeanUtil.copyProperties(page, voPage);
        List<ReportVenueDrawingCategoryAuditHistory> list = page.getRecords();
        if (list == null || list.isEmpty()) {
            return voPage;
        }
        List<ReportVenueDrawingCategoryAuditHistoryVO> voList = list
                .stream()
                .map(history -> BeanUtil.toBean(history, ReportVenueDrawingCategoryAuditHistoryVO.class))
                .collect(Collectors.toList());
        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    public Page<ReportVenueDrawingCategoryAuditHistory> listHistories(PageParams<ReportVenueDrawingCategoryAuditHistoryDTO> pageParams) {
        Page<ReportVenueDrawingCategoryAuditHistory> page = pageParams.buildPage();
        ReportVenueDrawingCategoryAuditHistoryDTO query = pageParams.getModel();
        QueryWrapper<ReportVenueDrawingCategoryAuditHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ReportVenueDrawingCategoryAuditHistory::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(StrUtil.isNotBlank(query.getReportVenueSpaceNumber()),
                        ReportVenueDrawingCategoryAuditHistory::getReportVenueSpaceNumber, query.getReportVenueSpaceNumber())
                .eq(query.getDrawingCategoryId() != null, ReportVenueDrawingCategoryAuditHistory::getDrawingCategoryId, query.getDrawingCategoryId())
                .orderByAsc(ReportVenueDrawingCategoryAuditHistory::getDrawingCategorySort)
        ;
        return page(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveReportVenueDrawingCategoryAuditHistory(ReportVenueDrawingCategoryAuditHistoryDTO saveDTO) {
        // 保存图纸分类审核历史
        ReportVenueDrawingCategoryAuditHistory history = BeanUtil.toBean(saveDTO, ReportVenueDrawingCategoryAuditHistory.class);
        save(history);
        // 批量保存图纸审核历史
        List<ReportVenueDrawingAuditHistoryDTO> reportVenueDrawingAuditHistoryDTOList = saveDTO.getReportVenueDrawingAuditHistoryDTOList();
        return reportVenueDrawingAuditHistoryService.saveBatch(reportVenueDrawingAuditHistoryDTOList, history.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<ReportVenueDrawingCategoryAuditHistoryDTO> list) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(this::saveReportVenueDrawingCategoryAuditHistory);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchByReportVenueDrawingCategoryAudit(List<ReportVenueDrawingCategoryAudit> categoryList, List<ReportVenueDrawingAudit> drawingList) {
        Map<Long, List<ReportVenueDrawingAudit>> reportVenueDrawingAuditMap = drawingList
                .stream()
                .collect(Collectors.groupingBy(ReportVenueDrawingAudit::getReportVenueDrawingCategoryAuditId));
        // 图纸分类审核列表转为图纸分类审核历史列表
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
        List<ReportVenueDrawingCategoryAuditHistoryDTO> historyDTOList = categoryList
                .stream()
                .map(audit -> {
                    ReportVenueDrawingCategoryAuditHistoryDTO historyDTO = BeanUtil.toBean(audit, ReportVenueDrawingCategoryAuditHistoryDTO.class, copyOptions);
                    historyDTO.setReportVenueSpaceDrawingCategoryAuditId(audit.getId());

                    List<ReportVenueDrawingAudit> reportDrawingAuditList = reportVenueDrawingAuditMap.get(audit.getId());
                    List<ReportVenueDrawingAuditHistoryDTO> reportDrawingAuditHistoryList = reportVenueDrawingAuditHistoryService.toReportVenueDrawingAuditHistoryDTOList(reportDrawingAuditList);
                    historyDTO.setReportVenueDrawingAuditHistoryDTOList(reportDrawingAuditHistoryList);
                    return historyDTO;
                })
                .collect(Collectors.toList());
        // 批量保存图纸分类审核历史
        return saveBatch(historyDTOList);
    }
}
